import pinia
from debug.Debug import CustomLogger, DebugType
from effect.util.console.SuperConsole import cls, wLine, readKey
from effect.util.console.playerdata_print import pprint
from entiys.methods.augument.SaveAttribute import SaveAttribute
from game import InnerMap
from game.inGame.Children.MapShow import MapManager
from game.inGame.Children.SlaveManager import SlaveManager
from game.TopClass import Windows


class GameIndex(Windows):
    def __init__(self, *args, **kwargs):
        super().__init__()

    @staticmethod
    def gohome(gameIndex):
        cls()
        print("你回到了家中...[任意键继续]")
        readKey()
        cls()
        where = gameIndex.saveattribute.getPlaydataJson();
        where['Where'] = "home"
        gameIndex.saveattribute.setPlaydataJson(where)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.hotKeyMapper = {
            'a': InnerMap,
            's': lambda: SlaveManager(lastWindows=self),
            'j': lambda: MapManager(saveattribute=self.saveattribute, lastWindows=self),
            "oem1": lambda: GameIndex.gohome(self),
            "；": lambda: GameIndex.gohome(self)
        }

    def UiShow(self):
        cls()
        self.Logic()  # 刷新
        wLine(val=f"存档名称: {self.saveattribute.saveName} 当前属性")
        pprint(self.playerdata)
        wLine(val="等待你的操作中...")
        print("a - 当前地点交互")
        print("s - 奴隶管理")
        print("d - 当前任务")
        print("f - 游戏设置")
        print("j - 打开地图")
        print("k - 领地管理")
        print("l - 训练奴隶")
        print("; - 回家")
        print("按下对应单键进行游玩吧!")
        key = readKey().lower()
        func = self.hotKeyMapper.get(key)
        if func is None:

            CustomLogger().log(f"用户在主菜单按住了{key} 这是一个没有用的键", DebugType.BOLD_STRONG)
            self.UiShow()
            return

        obj = func()
        if not isinstance(obj, Windows):  # 只是一个普通的函数 而不是窗口函数的时候只是简单的执行
            self.UiShow()
            return
        func().UiShow()

    def Logic(self):
        self.saveattribute = SaveAttribute(saveName=pinia.store.get("savename"))
        self.playerdata = self.saveattribute.getPlaydataJson()
